//
//  SetLockView.m
//  QRMaster
//
//  Created by myApple on 15/5/5.
//  Copyright (c) 2015年 myApple. All rights reserved.
//


#import "SetLockView.h"
#import "RequestNetWork.h"
#import "DropDownMenuView.h"
#import "PromptInfoView.h"
#import "BulidingModel.h"
#import "Reachability.h"
#import "ZXMultiFormatWriter.h"
#import "ZXImage.h"
#import "ZXingObjC.h"
#import "anaylsistDimensional.h"
#import <AudioToolbox/AudioToolbox.h>
#import "RoomsModel.h"
#import "BulidingInfoLogic.h"
#import "FloorsInfoLogic.h"
#import "RoomInfoLogic.h"
#import "FloorsModel.h"
#import "MJExtension.h"
#import "MZSelectableLabel.h"
#import "MBProgressHUD.h"
//缩放放大比例
#define SCALEWIDTH UI_SCREEN_WIDTH/bigHouseCardImgView.frame.size.width

@interface SetLockView()<DropDownMenuViewDelegate>
{
    
    UIScrollView * HouseScrollerView;
    
    UILabel * buildingLabel;
    UILabel * floorLabel;
    UILabel * roomLabel;
    
    NSMutableArray * buildingArray;
    NSMutableArray * floorArray;
    NSMutableArray * roomArray;
    
    BOOL FloorFlag;
    BOOL bulidFlag;
    BOOL roomFlag;
    
    UIButton * bulidingImgBtn;
    UIButton * FloorImgBtn;
    UIButton * roomFlagImgBtn;
    
    DropDownMenuView * ddmBView;
    DropDownMenuView * ddmFView;
    DropDownMenuView * ddmRView;
    
    UIButton * authorBtn;
    UIButton * setBtn;
    
    BOOL authorFlag;//纪录当前是授权卡还是配置卡
    
    NSString * buildingId;
    NSString * floorId;
    NSString * roomID;
    
    UILabel * titleLabel;
    UILabel * authorTypeLabel;
    UILabel * roomNameLabel;
    
    NSString * roomNumber;
    
    UIControl * backViewControl;
    UIImageView * HouseCardImgView;//二维码房卡
    UILabel * cardNumberLabel;//房卡号
    
    ZXImage * ZXimage;//二维码
    
    NSMutableDictionary * setDict;//获取二维码的请求体
    
     anaylsistDimensional * anaylsistDim;
    
    UIImageView * floatImg;//切换房卡类型的按钮图片
    
    UIView * promtnNoRoomView;
    
    UIImageView * logoImgView;
    UIView * setExplainView;//设置说明view
    UIView * setExplainBackView;//设置说明背景
    
    UIImageView * showImageView;
    float lastScale;
//    CGRect minImgRect;
//    CGRect maxImgRect;
    CGRect minHouseCardImgRect;
    CGRect maxHouseCardImgRect;
    CGRect minLogoImgRect;
    CGRect maxLogoImgRect;
    UIView * showView;
    

   
    UIImageView * bigHouseCardImgView;
    UIImageView * bigLogoImgView;
    UIImageView * ZXimageView;
}


@end

static SystemSoundID shake_sound_refresh = 1;
static CGRect houseCardOldFrame;
static CGRect logoOldFrame;

@implementation SetLockView

-(void)setCommunityId:(NSString *)communityId
{
    self.backgroundColor = UIColorFromRGB(0xebebeb);
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sharedTelePhone) name:@"shared" object:nil];
    bulidFlag = NO;
    FloorFlag = NO;
    roomFlag = NO;
    setDict = [[NSMutableDictionary alloc]init];
    buildingArray = [[NSMutableArray alloc]init];
    floorArray = [[NSMutableArray alloc]init];
    roomArray = [[NSMutableArray alloc]init];
    _communityId = communityId;
    self.backgroundColor = [UIColor whiteColor];
    [self prepareTitleView];
    [self prepareCardView];
    [self prepareDropView];
    [self prepareViewWithNoRoom];
    [self RequestNetWorkForRoom];
    [self getDataFromDisk];
    [self prepareSetExplainView];
//    [self showBigImage];

}

-(void)playRefreshSound
{
    NSString * path = [[NSBundle mainBundle]pathForResource:@"shake_match" ofType:@"mp3"];
    if (path) {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &shake_sound_refresh);
        AudioServicesPlayAlertSound(shake_sound_refresh);
    }
}



-(void)prepareTitleView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 0;
    float y = 0;
    float width = ScreenWidth / 3.0;
    float height = 44;
    
    //楼栋
    UIButton * bulidingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bulidingBtn.frame = CGRectMake(x, y, width, height);
    [bulidingBtn setBackgroundColor:[UIColor whiteColor]];
    [bulidingBtn addTarget:self action:@selector(bulidingClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:bulidingBtn];
    buildingLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, width-20 - 16, height)];
    buildingLabel.textAlignment = NSTextAlignmentCenter;
    buildingLabel.textColor = [UIColor blackColor];
    buildingLabel.font = [UIFont systemFontOfSize:14.0];
    [bulidingBtn addSubview:buildingLabel];
    
    x = width - 16;
    y = 19;
    width = 8;
    height = 7;
    bulidingImgBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [bulidingImgBtn setImage:[UIImage imageNamed:@"setLockDown"] forState:0];
    [bulidingBtn addSubview:bulidingImgBtn];
    
    x = bulidingBtn.frame.size.width - 1;
    y = 5;
    height = bulidingBtn.frame.size.height - 10;
    width = 1;
    UIImageView * bulidingLineView = [[UIImageView alloc]initWithFrame:CGRectMake(x,y,width,height)];
    bulidingLineView.image = [UIImage imageNamed:@"setLockLine"];
    [bulidingBtn addSubview:bulidingLineView];
    
    //楼层
    x = ScreenWidth / 3.0;
    y = 0;
    width = x;
    height = 44;
    UIButton * floorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    floorBtn.frame = CGRectMake(x, y, width, height);
    [floorBtn addTarget:self action:@selector(floorClick) forControlEvents:UIControlEventTouchUpInside];
    [floorBtn setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:floorBtn];
    floorLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, width-20 -16, height)];
    floorLabel.textAlignment = NSTextAlignmentCenter;
    floorLabel.textColor = [UIColor blackColor];
    floorLabel.font = [UIFont systemFontOfSize:14.0];
    [floorBtn addSubview:floorLabel];
    
    x = width - 16;
    y = 19;
    width = 8;
    height = 7;
    FloorImgBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [FloorImgBtn setImage:[UIImage imageNamed:@"setLockDown"] forState:0];
    [floorBtn addSubview:FloorImgBtn];
    
    x = floorBtn.frame.size.width - 1;
    y = 5;
    height = floorBtn.frame.size.height - 10;
    width = 1;
    UIImageView * floorLineView = [[UIImageView alloc]initWithFrame:CGRectMake(x,y,width,height)];
    floorLineView.image = [UIImage imageNamed:@"setLockLine"];
    [floorBtn addSubview:floorLineView];
    
    //房间
    x = ScreenWidth / 3.0  * 2;
    y = 0;
    width = ScreenWidth / 3.0;
    height = 44;
    UIButton * roomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    roomBtn.frame = CGRectMake(x, y, width, height);
    [roomBtn addTarget:self action:@selector(roomClick) forControlEvents:UIControlEventTouchUpInside];
    [roomBtn setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:roomBtn];
    roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, width-20-16, height)];
    roomLabel.textAlignment = NSTextAlignmentCenter;
    roomLabel.textColor = [UIColor blackColor];
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [roomBtn addSubview:roomLabel];
    
    x = width - 16;
    y = 19;
    width = 8;
    height = 7;
    roomFlagImgBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [roomFlagImgBtn setImage:[UIImage imageNamed:@"setLockDown"] forState:0];
    [roomBtn addSubview:roomFlagImgBtn];

    x = 0;
    width = UI_SCREEN_WIDTH;
    height = 56;
    y = roomBtn.frame.origin.y + roomBtn.frame.size.height + 16;
    UIView * showTypeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:showTypeView];
    showTypeView.layer.borderColor = UIColorFromRGB(0xc1c1c1).CGColor;
    showTypeView.layer.borderWidth = 0.5;
    showTypeView.backgroundColor = [UIColor whiteColor];
    
    x = 16;
    width = UI_SCREEN_WIDTH/2.0 - x;
    y = (56 - 20)/2.0;
    height = 20;
    authorTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    authorTypeLabel.text = NSLocalizedString(@"auothCard", @"授权卡");
    authorTypeLabel.font = [UIFont systemFontOfSize:16.0];
    [showTypeView addSubview:authorTypeLabel];
    
    x = UI_SCREEN_WIDTH /2.0 - 16;
    roomNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomNameLabel.font = [UIFont systemFontOfSize:16.0];
    roomNameLabel.textAlignment = NSTextAlignmentRight;
    [showTypeView addSubview:roomNameLabel];
}


-(void)sharedTelePhone
{
    [self refreshCard];
}

#pragma mark -- 准备下拉列表
-(void)prepareDropView
{
    float x = 0;
    float y = 104;
    float width = [UIScreen mainScreen].bounds.size.width;
    float height = 0;
    
    //底部view让其下拉消失
    backViewControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 40, width, UI_SCREEN_HEIGHT - y)];
    backViewControl.alpha = 0.5;
    backViewControl.hidden = YES;
    backViewControl.backgroundColor = UIColorFromRGB(0x000000);
    [backViewControl addTarget:self action:@selector(removeDropDown) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backViewControl];
    
    ddmBView = [[DropDownMenuView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    ddmBView.delegate = self;
    ddmRView.hidden = YES;
    ddmBView.data = buildingArray;
    [backViewControl addSubview:ddmBView];
    

    ddmFView = [[DropDownMenuView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    ddmFView.delegate = self;
    ddmFView.data = floorArray;
    ddmFView.hidden = YES;
    [backViewControl addSubview:ddmFView];
    
    ddmRView = [[DropDownMenuView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    ddmRView.delegate = self;
    ddmRView.data = roomArray;
    ddmRView.hidden = YES;
    [backViewControl addSubview:ddmRView];
    
}

#pragma mark -----------点击背景隐藏tableview
-(void)removeDropDown
{
    [self bulidingSelect:YES];
    [self floorSelect:YES];
    [self roomSelect:YES];
    [ddmBView showOrNotList:YES];
    [ddmFView showOrNotList:YES];
    [ddmRView showOrNotList:YES];
    bulidFlag = NO;
    FloorFlag = NO;
    roomFlag = NO;
    backViewControl.hidden = YES;

}

#pragma mark ---------楼栋、楼层、房间的选中和未选中状态
-(void)bulidingSelect:(BOOL)flag
{
    if (!flag) {
        [bulidingImgBtn setImage:[UIImage imageNamed:@"setLockSelectTirUp"] forState:0];
        buildingLabel.textColor = UIColorFromRGB(0x7dc234);
    }
    else
    {
        [bulidingImgBtn setImage:[UIImage imageNamed:@"setLockDown"] forState:0];
        buildingLabel.textColor = [UIColor blackColor];
    }
}
-(void)floorSelect:(BOOL)flag
{
    if (!flag) {
        [self bulidingSelect:YES];
        [FloorImgBtn setImage:[UIImage imageNamed:@"setLockSelectTirUp"] forState:0];
        floorLabel.textColor = UIColorFromRGB(0x7dc234);
    }
    else
    {
        [FloorImgBtn setImage:[UIImage imageNamed:@"setLockDown"] forState:0];
        floorLabel.textColor = [UIColor blackColor];
    }
}
-(void)roomSelect:(BOOL)flag
{
    if (!flag) {
        [self floorSelect:YES];
        [roomFlagImgBtn setImage:[UIImage imageNamed:@"setLockSelectTirUp"] forState:0];
        roomLabel.textColor = UIColorFromRGB(0x7dc234);
    }
    else
    {
       [roomFlagImgBtn setImage:[UIImage imageNamed:@"setLockDown"] forState:0];
        roomLabel.textColor = [UIColor blackColor];
    }
}

#pragma mark ------------ 选择楼栋，楼层
-(void)bulidingClick
{
    if (bulidFlag) {
        backViewControl.hidden = YES;
        
    }
    else
    {
        backViewControl.hidden = NO;
    }
     [self bulidingSelect:bulidFlag];
    FloorFlag = YES;
    roomFlag = YES;
    [ddmFView showOrNotList:FloorFlag];
    [ddmRView showOrNotList:roomFlag];
    FloorFlag = NO;
    roomFlag = NO;
    NSMutableArray * bulidNameArray = [[NSMutableArray alloc]init];
    int count = (int) buildingArray.count;
    for (int i = 0; i<count; i++)
    {
        BulidingModel * model = buildingArray[i];
        [bulidNameArray addObject:model.buildingName];
    }
    ddmBView.data = bulidNameArray;
    [ddmBView showOrNotList:bulidFlag];
    bulidFlag = !bulidFlag;
    
}

-(void)floorClick
{
    if (FloorFlag) {
        backViewControl.hidden = YES;
    }
    else
    {
        backViewControl.hidden = NO;
    }
     [self floorSelect:FloorFlag];
    bulidFlag = YES;
    roomFlag = YES;
    [ddmBView showOrNotList:bulidFlag];
    [ddmRView showOrNotList:roomFlag];
    bulidFlag = NO;
    roomFlag = NO;
    NSMutableArray * floorNameArray = [[NSMutableArray alloc]init];
    int count = (int) floorArray.count;
    for (int i = 0; i< count; i++)
    {
        FloorsModel * model = floorArray[i];
        [floorNameArray addObject:model.floorName];
    }
    ddmFView.data = floorNameArray;
    [ddmFView showOrNotList:FloorFlag];
    FloorFlag = !FloorFlag;
    
}

-(void)roomClick
{
    if (roomFlag) {
        backViewControl.hidden = YES;
    }
    else
    {
        backViewControl.hidden = NO;
    }
     [self roomSelect:roomFlag];
    bulidFlag = YES;
    FloorFlag = YES;
    [ddmBView showOrNotList:bulidFlag];
    [ddmFView showOrNotList:FloorFlag];
    bulidFlag = NO;
    FloorFlag = NO;
    NSMutableArray * roomNameArray = [[NSMutableArray alloc]init];
    int count = (int) roomArray.count;
    for (int i = 0; i< count; i++)
    {
        RoomsModel * model = roomArray[i] ;
        [roomNameArray addObject:model.roomName];
    }
    ddmRView.data = roomNameArray;
    [ddmRView showOrNotList:roomFlag];
    roomFlag = !roomFlag;
}


#pragma mark --------- 请求房间数据
-(void)RequestNetWorkForRoom
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:_communityId forKey:@"communityId"];
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    __weak SetLockView * weakSelf = self;
    [rnw initWithPath:POST(HOTELINFO) params:postDict block:^(id downLoadData, id error) {
        NSArray * data = [downLoadData objectForKey:@"data"];
        if (data) {
            [weakSelf AnalysisHouseData:data];
        }
        else
        {
            if ([[error objectForKey:@"status"]intValue] == 301) {
                promtnNoRoomView.hidden = NO;
                [MBProgressHUD hideAllHUDsForView:self animated:YES];
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(noRoomInCommunity)]) {
                    [weakSelf.delegate noRoomInCommunity];
                }
            }

            [PromptInfoView showMessage:[error objectForKey:@"info"]];
            
        }
    }];
   
}

-(void)AnalysisHouseData:(NSArray *)data
{
    if (data != nil) {
        [self insertDataIntoRooms:data];
    }
    [buildingArray removeAllObjects];
    for (NSDictionary * dict in data)
    {
        BulidingModel * bModel = [[BulidingModel alloc]init];
        bModel.buildingName = [dict objectForKey:@"buildingname"];
        bModel.buildingId = [dict objectForKey:@"buildingid"];
        [floorArray removeAllObjects];
        for (NSDictionary * fDict in [dict objectForKey:@"floors"])
        {
            FloorsModel * fModel = [[FloorsModel alloc]init];
            fModel.floorId = [fDict objectForKey:@"floorid"];
            fModel.floorName = [fDict objectForKey:@"floorname"];
             [roomArray removeAllObjects];
            for (NSDictionary * dict in [fDict objectForKey:@"rooms"])
            {
                RoomsModel * rModel = [[RoomsModel alloc]init];
                rModel.roomId = [dict objectForKey:@"roomid"];
                rModel.roomName = [dict objectForKey:@"roomname"];
                [roomArray addObject:rModel];
                
            }
            fModel.rooms = [NSMutableArray arrayWithArray:roomArray];
//            [fModel.rooms addObject:roomArray];
            [floorArray addObject:fModel];
        }
        bModel.floors = [NSMutableArray arrayWithArray:floorArray];
//        [bModel.floors addObject:floorArray];
      [buildingArray addObject:bModel];
    }
    BulidingModel * newModel = buildingArray[0];
    buildingLabel.text = newModel.buildingName;
    buildingId = newModel.buildingId;
    floorArray = [NSMutableArray arrayWithArray:newModel.floors];
    FloorsModel * newFModel = newModel.floors[0];
    floorLabel.text = newFModel.floorName;
    floorId = newFModel.floorId;
    roomArray = [NSMutableArray arrayWithArray:newFModel.rooms];
    RoomsModel * newRModel = newFModel.rooms[0];
    roomLabel.text = newRModel.roomName;
    roomNameLabel.text = newRModel.roomName;
    roomID = newRModel.roomId;
    
    [self getcheckAuthData:@"2"];
    authorFlag = YES;
    if (authorFlag) {
        titleLabel.text = NSLocalizedString(@"auothCard", nil);
    }
}

//根据communityId从本地获取数据，并以BFmodel的形式存入数组，返回
-(void)getDataFromDisk
    {
        BulidingInfoLogic * bulidingDataBase = [[BulidingInfoLogic alloc]init];
        [bulidingDataBase queryBulidingInfo:_communityId block:^(NSMutableArray * bulidArray)
         {
             if (bulidArray.count > 0 )
             {
                 BulidingModel * model = bulidArray[0];
                 buildingId = model.buildingId;
                 buildingArray = bulidArray;
                 buildingLabel.text = model.buildingName;
             }
             
         }];
        
        FloorsInfoLogic * floorDataBase = [[FloorsInfoLogic alloc]init];
        [floorDataBase queryFloorsInfo:buildingId block:^(NSMutableArray *arr)
         {
             if (arr.count > 0 ) {
                 FloorsModel * model = arr[0];
                 floorId = model.floorId;
                 floorArray = arr;
                 floorLabel.text = model.floorName;
             }
        }];
        
        RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
        [roomDataBase queryRoomInfo:floorId block:^(NSMutableArray * roomResultArray)
         {
             if (roomResultArray.count > 0 ) {
                 RoomsModel * model = roomResultArray[0];
                 roomID = model.roomId;
                 roomArray = roomResultArray;
                 roomLabel.text = model.roomName;
                 roomNameLabel.text = model.roomName;
                 cardNumberLabel.text =model.roomName;
             }
         }];
}

#pragma mark --- 显示二维码
-(void)prepareCardView
{
    float x = 0;
    float y = 132;
    float width = UI_SCREEN_WIDTH;
    float height = 268;
    UIImageView * houseCardBackView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    houseCardBackView.userInteractionEnabled = YES;
    houseCardBackView.backgroundColor = [UIColor whiteColor];
    [self addSubview:houseCardBackView];
    
    //二维码房卡
    x = (UI_SCREEN_WIDTH - 180)/2.0;
    y = 16;
    width = 180;
    height = 180;
    HouseCardImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    HouseCardImgView.backgroundColor = [UIColor whiteColor];
    HouseCardImgView.userInteractionEnabled = YES;
    [houseCardBackView addSubview:HouseCardImgView];

    UIGestureRecognizer * showBigImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigImage)];
    [HouseCardImgView addGestureRecognizer:showBigImage];
    
    logoImgView = [[UIImageView alloc]initWithFrame:CGRectMake(HouseCardImgView.frame.size.width / 2.0 - 15, HouseCardImgView.frame.size.height / 2.0 - 15, 30, 30)];
    logoImgView.image = [UIImage imageNamed:@"ic_锁掌柜_70"];
    logoImgView.hidden = YES;
    [HouseCardImgView addSubview:logoImgView];
    
    //刷新
    x = UI_SCREEN_WIDTH - 45;
    y = 7;
    width = 40;
    height = 40;
    UIView * refreshView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [houseCardBackView addSubview:refreshView];
    UITapGestureRecognizer * refreshTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(refreshCard)];
    [refreshView addGestureRecognizer:refreshTap];
    
    x = 9;
    y = 9;
    width = 22;
    height = 22;
    UIImageView * refreshImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    refreshImageView.image = [UIImage imageNamed:@"setLockRefresh"];
    [refreshView addSubview:refreshImageView];
    
    x = 16;
    y = 212;
    width = UI_SCREEN_WIDTH - 2 * x;
    height = 0.5;
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    lineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [houseCardBackView addSubview:lineView];
    
    x = 16;
    y += height + (56 - 22)/2.0;
    width = 22;
    height = 22;
    UIImageView * promtImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtImgView.layer.cornerRadius = 11;
    promtImgView.image = [UIImage imageNamed:@"setLockTips"];
    [houseCardBackView addSubview:promtImgView];
    
    x += width + 16;
    y -= 9;
    width = UI_SCREEN_WIDTH - x - 16;
    height = 40;
    MZSelectableLabel *mzTextLabel = [[MZSelectableLabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    mzTextLabel.text = NSLocalizedString(@"setPromt", nil);
    mzTextLabel.numberOfLines = 2;
    mzTextLabel.font = [UIFont systemFontOfSize:12.0];
    [houseCardBackView addSubview:mzTextLabel];
    __weak SetLockView * weakSelf = self;
    mzTextLabel.selectionHandler = ^(NSRange range,NSString * string)
    {
        [weakSelf promtSetView];
    };
    NSMutableAttributedString * astr = [[NSMutableAttributedString alloc]initWithString:mzTextLabel.text];
    [astr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12.0],NSFontAttributeName,UIColorFromRGB(0x2db7f5),NSForegroundColorAttributeName, nil] range:[mzTextLabel.text rangeOfString:NSLocalizedString(@"setExplanation", nil)]];
    mzTextLabel.attributedText = astr;
    [mzTextLabel setSelectableRange:[[mzTextLabel.attributedText string]rangeOfString:NSLocalizedString(@"setExplanation", nil)]];
    
}

-(void)prepareSetExplainView
{
    float x = 0;
    float y = 0;
    float width = UI_SCREEN_WIDTH;
    float height = UI_SCREEN_HEIGHT;
    
    setExplainBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setExplainBackView.backgroundColor = UIColorFromRGBAndA(0x000000, 0.5);
    setExplainBackView.hidden = YES;
    UITapGestureRecognizer * explainTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenPromtView)];
    [setExplainBackView addGestureRecognizer:explainTap];
    
    y = UI_SCREEN_HEIGHT;
    height = 180;
    setExplainView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setExplainView.backgroundColor = [UIColor whiteColor];
    [setExplainBackView addSubview:setExplainView];
    
    x = 16;
    y = 30;
    width = UI_SCREEN_WIDTH - x;
    height = 20;
    UILabel * tipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    tipsLabel.font = [UIFont systemFontOfSize:14.0];
    tipsLabel.text = NSLocalizedString(@"setExplanation", nil);
    [setExplainView addSubview:tipsLabel];
    
    y += height;
    height = 110;
    UILabel * explainLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    explainLabel.font = [UIFont systemFontOfSize:12.0];
    explainLabel.numberOfLines = 0;
    explainLabel.text = NSLocalizedString(@"detailExpalation", nil);
    [setExplainView addSubview:explainLabel];
}

#pragma mark ---------- 设置说明介绍
-(void)promtSetView
{
    [self showPromtView];
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:setExplainBackView];
    setExplainBackView.hidden = NO;
}

//帮助提示
-(void)showPromtView
{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = setExplainView.frame;
        frame.origin.y = UI_SCREEN_HEIGHT - setExplainView.frame.size.height;
        setExplainView.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}

//移除帮助提示
-(void)hiddenPromtView
{
   
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = setExplainView.frame;
        frame.origin.y = UI_SCREEN_HEIGHT;
        setExplainView.frame = frame;
    } completion:^(BOOL finished) {
         setExplainBackView.hidden = YES;
        [setExplainBackView removeFromSuperview];
    }];
}


#pragma mark --------- 切换房卡类型
-(void)getcheckAuthData:(NSString *)type
{
    if ([self IsHaveNetwork]) {
        return;
    }
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    [setDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [setDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [setDict setValue:[NSString stringWithFormat:@"0"] forKey:@"validTime"];
    if ([type isEqualToString:@"4"]) {
        [setDict setValue:[NSString stringWithFormat:@"5"] forKey:@"validTime"];
    }
    [setDict setValue:type forKey:@"type"];
    [setDict setValue:_communityId forKey:@"communityId"];
    [setDict setValue:buildingId forKey:@"buildingId"];
    [setDict setValue:floorId forKey:@"floorId"];
    [setDict setValue:roomID forKey:@"roomId"];
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    __weak SetLockView * weakSelf = self;
    [rnw initWithPath:POST(CHECKAUTHORTIY) params:setDict block:^(id downLoadData, id error)
     {
         [MBProgressHUD hideAllHUDsForView:self animated:YES];
         NSDictionary * data = [downLoadData objectForKey:@"data"];
         if (data)
         {
             if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"sound"]isEqualToString:@"1"]) {
                 [self playRefreshSound];
            }
             NSString * string = [data objectForKey:@"cardString"];
             anaylsistDim = [[anaylsistDimensional alloc]init];
             NSString * encodeString = [anaylsistDim stringFromHexString:string];
             ZXimage = [anaylsistDim prepareCardView:encodeString addImgView:HouseCardImgView];
             if (ZXimage)
             {
                 HouseCardImgView.image = [UIImage imageWithCGImage:ZXimage.cgimage];
                 logoImgView.hidden = NO;
             }
             else
             {
                 HouseCardImgView.image = [UIImage imageNamed:@"splash"];
                 logoImgView.hidden = YES;
             }
             
             if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(nowAuthorType:)]) {
                 [weakSelf.delegate nowAuthorType:authorFlag == 0?NSLocalizedString(@"auoth", nil):NSLocalizedString(@"config", nil)];
             }
             if (authorFlag) {
                 authorTypeLabel.text = NSLocalizedString(@"auothCard", nil);
             }
             else
             {
                  authorTypeLabel.text =NSLocalizedString(@"configCard", nil);
             }
         }
         else
         {
             
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
         }
    }];
}



//刷新房卡
-(void)refreshCard
{
    if (authorFlag) {
        [self getcheckAuthData:@"2"];
        
    }
    else
    {
        [self getcheckAuthData:@"4"];
    }
}

#pragma mark  -- 切换房卡类型
-(void)changeAuthorType
{
    authorFlag = !authorFlag;
    [self refreshCard];
}


#pragma ddmviewDelegate
-(void)DropDownMenuView:(DropDownMenuView *)menuView showIndex:(int)index back:(BOOL)flag
{
    if(menuView == ddmBView)
    {
        bulidFlag = flag;
        [floorArray removeAllObjects];
        [roomArray removeAllObjects];
        BulidingModel * bModel = buildingArray[index];
        buildingId = bModel.buildingId;
        buildingLabel.text = bModel.buildingName;
        FloorsInfoLogic * floorDataBase = [[FloorsInfoLogic alloc]init];
        [floorDataBase queryFloorsInfo:buildingId block:^(NSMutableArray *arr)
         {
             floorArray = arr;
         }];
       
        if (floorArray.count > 0) {
            FloorsModel * fModel = floorArray[0];
            floorLabel.text = fModel.floorName;
            floorId = fModel.floorId;
                RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
                [roomDataBase queryRoomInfo:fModel.floorId block:^(NSMutableArray * roomResultArray) {
                    roomArray = roomResultArray;
                }];

            if (roomArray.count > 0) {
                RoomsModel * rModel = roomArray[0];
                roomID = rModel.roomId;
                roomLabel.text = rModel.roomName;
                roomNameLabel.text = rModel.roomName;

            }
        }
        [self floorClick];
       
    }
    else if (menuView == ddmFView)
    {
        FloorFlag = flag;
        [roomArray removeAllObjects];//可变数组中赋值注意（会释放给它对象的内存）
        FloorsModel * fModel = floorArray[index];
        floorLabel.text = fModel.floorName;
        floorId = fModel.floorId;
        RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
        [roomDataBase queryRoomInfo:fModel.floorId block:^(NSMutableArray * roomResultArray) {
            roomArray = roomResultArray;
        }];
        if (roomArray.count > 0) {
            RoomsModel * rModel = roomArray[0];
            roomID = rModel.roomId;
            roomLabel.text = rModel.roomName;
            roomNameLabel.text = rModel.roomName;
        }
        [self roomClick];
    }
    else
    {
        [self roomSelect:YES];
        RoomsModel * rModel = roomArray[index];
        roomID = rModel.roomId;
        roomLabel.text = rModel.roomName;
        cardNumberLabel.text = rModel.roomName;
        roomNameLabel.text = rModel.roomName;
        backViewControl.hidden = YES;
    }
    
    [self getcheckAuthData:@"2"];
    authorFlag = YES;
    titleLabel.text = NSLocalizedString(@"auothCard", nil);
}

#pragma mark ---------- 没有房间的现实
-(void)prepareViewWithNoRoom
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = self.frame.size.height;
    float width;
    float height;
    
    promtnNoRoomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    promtnNoRoomView.backgroundColor = RGBA(237, 238, 241, 1);
    promtnNoRoomView.hidden = YES;
    [self addSubview:promtnNoRoomView];
    
    NSString * imgPath = [[NSBundle mainBundle]pathForResource:@"没有设置默认房卡" ofType:@"png"];
    UIImage * promImg = [UIImage imageWithContentsOfFile:imgPath];
    height= promImg.size.height * ScreenWidth / 3.0 /promImg.size.width;
    width = ScreenWidth / 3.0;
    UIImageView * promImgView = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width - width)/ 2.0, (self.frame.size.height - height)/ 2.0 - 64, width, height)];
    promImgView.image = promImg;
    [promtnNoRoomView addSubview:promImgView];
    
    
    UILabel * promtTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, promImgView.frame.size.height /2.0+promImgView.frame.origin.y + 74, ScreenWidth - 100, 60)];
    promtTextLabel.text = NSLocalizedString(@"noRoomInCommunity", nil);
    promtTextLabel.textColor = RGBA(211, 211, 211, 1);
    promtTextLabel.textAlignment = NSTextAlignmentCenter;
    promtTextLabel.numberOfLines = 0;
    promtTextLabel.font = [UIFont systemFontOfSize:16.0];
    [promtnNoRoomView addSubview:promtTextLabel];
    
}

#pragma mark ---------- 显示大图并可以缩放
- (void)bigImage{
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    houseCardOldFrame=[HouseCardImgView convertRect:HouseCardImgView.bounds toView:window];
    logoOldFrame=[logoImgView convertRect:logoImgView.bounds toView:window];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0;
    
    bigHouseCardImgView = [[UIImageView alloc]initWithFrame:houseCardOldFrame];
    UIImage * image = [UIImage imageWithCGImage:ZXimage.cgimage];
    bigHouseCardImgView.image = image;
    bigHouseCardImgView.tag = 1;
    bigHouseCardImgView.userInteractionEnabled = YES;

    bigLogoImgView = [[UIImageView alloc] initWithFrame:logoOldFrame];
    bigLogoImgView.image = [UIImage imageNamed:@"ic_锁掌柜_70"];
    bigLogoImgView.tag = 2;
    [backgroundView addSubview:bigHouseCardImgView];
    [backgroundView addSubview:bigLogoImgView];
    [window addSubview:backgroundView];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer: tap];
    
    UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scale:)];
    [backgroundView addGestureRecognizer:pinchRecognizer];
    
    [UIView animateWithDuration:0.5 animations:^{
        bigHouseCardImgView.frame = CGRectMake( ([UIScreen mainScreen].bounds.size.width-200)/2,([UIScreen mainScreen].bounds.size.height-200)/2, 200, 200);
        bigLogoImgView.frame = CGRectMake( ([UIScreen mainScreen].bounds.size.width-30)/2,([UIScreen mainScreen].bounds.size.height-30)/2, 30, 30);
        backgroundView.alpha=1.0;
    } completion:^(BOOL finished) {
        
    }];
    
    minHouseCardImgRect = CGRectMake((UI_SCREEN_WIDTH - 92)/2.0, (UI_SCREEN_HEIGHT - 92)/2.0, 92, 92);
    maxHouseCardImgRect = CGRectMake(0, (UI_SCREEN_HEIGHT - UI_SCREEN_WIDTH)/2.0, UI_SCREEN_WIDTH, UI_SCREEN_WIDTH);
    
    minLogoImgRect = CGRectMake((backgroundView.frame.size.width-15)/2, (backgroundView.frame.size.height - 15)/2, 15, 15);
    maxLogoImgRect = CGRectMake((UI_SCREEN_WIDTH-SCALEWIDTH*bigLogoImgView.frame.size.width)/2, (UI_SCREEN_HEIGHT-SCALEWIDTH*bigLogoImgView.frame.size.width)/2, SCALEWIDTH*bigLogoImgView.frame.size.width, SCALEWIDTH*bigLogoImgView.frame.size.width);

}

- (void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=houseCardOldFrame;
        backgroundView.alpha=0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}


//缩放
-(void)scale:(UIPinchGestureRecognizer *)pinch
{
    if ([pinch state] == UIGestureRecognizerStateEnded) {
        
        lastScale = 1.0;
        //缩放的大小控制
        if (bigHouseCardImgView.frame.size.width < 92) {
            bigHouseCardImgView.frame = minHouseCardImgRect;
             bigLogoImgView.frame = minLogoImgRect;

        }
        if (bigHouseCardImgView.frame.size.width > [UIScreen mainScreen].bounds.size.width) {
            bigHouseCardImgView.frame = maxHouseCardImgRect;
            bigLogoImgView.frame = maxLogoImgRect;

        }
        return;
    }
    

    CGFloat scale = 1.0 - (lastScale - [pinch scale]);

    CGAffineTransform currentTransform = bigHouseCardImgView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [bigHouseCardImgView setTransform:newTransform];
    CGAffineTransform currentTransform2 = bigLogoImgView.transform;
    CGAffineTransform newTransform2 = CGAffineTransformScale(currentTransform2, scale, scale);
    [bigLogoImgView setTransform:newTransform2];
    
    lastScale = [pinch scale];
}

#pragma mark ------ 隐藏大图
-(void)hiddenImageView:(UITapGestureRecognizer *)tap
{
    showView.hidden = YES;
}

#pragma mark -------- 显示大图
-(void)showBigImageView
{
    showImageView.image = [UIImage imageWithCGImage:ZXimage.cgimage];
    showView.hidden = NO;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//判断是否有网络
-(BOOL)IsHaveNetwork
{
    Reachability * reach = [Reachability reachabilityForInternetConnection];
    if([reach currentReachabilityStatus] == NotReachable) {
        [PromptInfoView showMessage:NSLocalizedString(@"noNetWork", nil)];
        return YES;
    }
    return NO;
}


#pragma mark -------- 存房间数据
-(void)insertDataIntoRooms:(NSArray *)allBuildingInfo
{
    BulidingInfoLogic * bulidingDataBase = [[BulidingInfoLogic alloc]init];
    FloorsInfoLogic * floorDataBase = [[FloorsInfoLogic alloc]init];
    RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
    [bulidingDataBase deleteBulidingWithCommunityId:_communityId];
    [floorDataBase deleteFloorsInfoWithCommunityId:_communityId];
    [roomDataBase deleteRoomWithCommunityId:_communityId];
    for (NSDictionary * bDict in allBuildingInfo)
    {
        BulidingModel * bModel = [[BulidingModel alloc]init];
        bModel.buildingName = [bDict objectForKey:@"buildingname"];
        bModel.buildingId = [bDict objectForKey:@"buildingid"];
        bModel.buildingNo = [bDict objectForKey:@"buildingno"];
        [bulidingDataBase insertBulidingInfo:bModel withCommunityId:_communityId];
        bModel.floors = [bDict objectForKey:@"floors"];
        for (NSDictionary * fDict in bModel.floors) {
            FloorsModel * fModel = [[FloorsModel alloc]init];
            fModel.floorId = [fDict objectForKey:@"floorid"];
            fModel.floorName = [fDict objectForKey:@"floorname"];
            fModel.floorNo  = [fDict objectForKey:@"floorno"];
            [floorDataBase insertFloorInfo:fModel withCommunityId:_communityId buildingId:bModel.buildingId];
            fModel.rooms = [fDict objectForKey:@"rooms"];
            for (NSDictionary * rDict in fModel.rooms) {
                RoomsModel * rModel = [[RoomsModel alloc]init];
                rModel.roomId = [rDict objectForKey:@"roomid"];
                rModel.roomName = [rDict objectForKey:@"roomname"];
                rModel.roomNo = [rDict objectForKey:@"roomno"];
                rModel.isSelect = [rDict objectForKey:@"isSelected"];
                rModel.lockNo = [rDict objectForKey:@"lockno"];
                rModel.occupation = [rDict objectForKey:@"occupation"];
                [roomDataBase insertRoomInfo:rModel withFloorId:fModel.floorId withCommunityId:_communityId];
            }
        }
    }
    
}

-(NSString *)getStr:(NSString *)str
{
    NSString *returnStr=@"";
    if (str==nil || [@"" isEqualToString:str]==YES || str.length==0 ) {
        returnStr=@"";
    }
    else
    {
        returnStr=str;
    }
    return returnStr;
}

@end
